package com.ashen.boot.admin.project.system.gs.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Maps;
import com.ashen.boot.admin.annotation.ClassDescribe;
import com.ashen.boot.admin.annotation.Log;
import com.ashen.boot.admin.annotation.PreAuth;
import com.ashen.boot.admin.commons.enums.BusinessTypeEnum;
import com.ashen.boot.admin.domain.controller.SuperSimpleBaseController;
import com.ashen.boot.admin.domain.controller.base.IBasePoiController;
import com.ashen.boot.admin.domain.result.R;
import com.ashen.boot.admin.domain.result.Result;
import com.ashen.boot.admin.excel.listener.UploadDataListener;
import com.ashen.boot.admin.excel.utils.ExcelConstant;
import com.ashen.boot.admin.excel.utils.ExcelUtils;
import com.ashen.boot.admin.exceptions.ExportExceptions;
import com.ashen.boot.admin.project.system.gs.model.dto.InfoGsDTO;
import com.ashen.boot.admin.project.system.gs.model.dto.InfoGsExcelDTO;
import com.ashen.boot.admin.project.system.gs.model.entity.InfoGsEntity;
import com.ashen.boot.admin.project.system.gs.model.query.InfoGsParams;
import com.ashen.boot.admin.project.system.gs.service.IInfoGsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gao
 * @since 2021-10-28
 */
@RestController
@RequestMapping("/api/v3/system/gs")
@PreAuth("gs")
@ClassDescribe("公司管理")
public class InfoGsController extends SuperSimpleBaseController<Long, InfoGsDTO, InfoGsParams, InfoGsEntity> implements IBasePoiController<InfoGsParams> {
    private final IInfoGsService service;

    public InfoGsController(IInfoGsService service) {
        super(service);
        this.service = service;
    }


    /**
     * 导出
     *
     * @param response 响应
     * @param params   请求参数,用于导出过滤
     * @throws ExportExceptions 导出异常
     */
    @Override
    @PostMapping("/export")
    @Log(value = "导出", paramsName = {"params"}, businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("hasAnyAuthority('ROLE_ADMINISTRATOR','infoGs:export')")
    public void export(HttpServletResponse response, @RequestBody InfoGsParams params) throws ExportExceptions {
        List<InfoGsExcelDTO> export = service.export(params);
        HashMap<String, Object> map = Maps.newHashMap();
        try {
            map.put(ExcelConstant.FILE_NAME, "post_excel");
            map.put(ExcelConstant.DATA_LIST, export);
            ExcelUtils.writeWeb(response, map, ExcelTypeEnum.XLS, InfoGsExcelDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExportExceptions("岗位导出失败", e);
        }
    }

    @Override
    @PostMapping("/import")
    @Log(value = "导入", businessType = BusinessTypeEnum.IMPORT)
    @PreAuthorize("hasAnyAuthority('ROLE_ADMINISTRATOR','infoGs:upload')")
    public Result<String> upload(MultipartFile file) throws IOException {
        ExcelReader excelReader = EasyExcel.read(file.getInputStream(), InfoGsExcelDTO.class, new UploadDataListener<InfoGsExcelDTO>(service)).build();
        excelReader.read(EasyExcel.readSheet(0).build());
        excelReader.finish();
        return R.success("导入成功");
    }


//    @PostMapping("/import")
//    public Result<String> exc(MultipartFile file, HttpServletRequest request) {
//
//        // 判断文件是否为空
//        if (file == null) {
//            return  R.fail("上传学生文件不能为空");
//        }
//
//        // 2.判断上传内容是否符合要求
//        String fileName = file.getOriginalFilename();
//
//        // 判断是否是excel文件
//        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
//            return R.fail("请上传正确格式的文件");
//        }
//
//        String errMsg = service.batchImport(file);
//
//        if (StringUtils.isEmpty(errMsg)) {
//            return R.success("导入成功");
//        }else {
//            return R.fail(errMsg);
//        }
//    }

}

